<?php
namespace app\common\controller;

use think\Controller;
use think\Loader;

class Base extends Controller
{

    public function _initialize()
    {
        parent::_initialize();
    }

    static public function showReturnCode($code='',$data=[],$msg='')
    {
        $return_data =[
            'code'   =>'500',
            'msg'   =>'未定义消息',
            'data'  => $code == 1001 ? $data : [],
        ];
        if(empty($code)) return $return_data;
        $return_data['code']= $code;
        if(!empty($msg)){
            $return_data['msg'] = $msg;
        }else if (isset(ReturnCode::$return_code[$code])){
            $return_data['msg'] = ReturnCode::$return_code[$code];
        }
        return $return_data;
    }

    static public function showReturnCodeWithOutData($code='',$msg='')
    {
        return self::showReturnCode($code,[],$msg);
    }


    /**
     * 数据库字段 网页字段转换
     * #User:
     * #Email:@qq.com
     * #Date:
     * @param $array 转化数组
     * @return 返回数据数组
     */
    protected function buildParam($array=[])
    {
        $data=[];
        if (is_array($array)&&!empty($array)){
            foreach( $array as $item=>$value ){
                $data[$item] = $this->request->param($value);
            }
        }
        return $data;
    }

    /**
     * 快速调用执行model函数  默认执行$action_name='editData'
     * Power:
     * Email：@qq.com
     * @param $param_data
     * @param bool|false $validate_name
     * @param bool|false $model_name
     * @param string $action_name
     * @return array
     */
    protected function doModelAction($param_data,$validate_name = false, $model_name = false,$action_name=''){
        if ($validate_name != false) {
            $result = $this->validate($param_data, $validate_name);
            if (true !== $result) return $this->showReturnCode(1003, [], $result);
        }
        $model_edit = Loader::model($model_name);
        if (!$model_edit) return $this->showReturnCode(1010);
        return $model_edit->$action_name($param_data);
    }



}
